#include "friend.h"
#include "protocol.h"
#include "tcpclient.h"
#include <QInputDialog>
#include <QMessageBox>
#include "privatechat.h"

Friend::Friend(QWidget *parent)
    : QWidget{parent}
{
    m_pShowMsgTE = new QTextEdit;
    m_pFriendListWidget = new QListWidget;
    m_pInputMsgLE = new QLineEdit;

    m_pDelFriendPB = new QPushButton("删除好友");
    m_pFlushFriendPB = new QPushButton("刷新好友");
    m_pShowOnlieUsrPB = new QPushButton("显示在线用户");
    m_pSearchUsrPB = new QPushButton("查找用户");
    m_pMsgSendPB = new QPushButton("信息发送");
    m_pPrivateChatPB = new QPushButton("私聊");

    QVBoxLayout * pRightPBVBL = new QVBoxLayout;
    pRightPBVBL->addWidget(m_pDelFriendPB);
    pRightPBVBL->addWidget(m_pFlushFriendPB);
    pRightPBVBL->addWidget(m_pShowOnlieUsrPB);
    pRightPBVBL->addWidget(m_pSearchUsrPB);
    pRightPBVBL->addWidget(m_pPrivateChatPB);

    QHBoxLayout *pTopHBL = new QHBoxLayout;
    pTopHBL->addWidget(m_pShowMsgTE);
    pTopHBL->addWidget(m_pFriendListWidget);
    pTopHBL->addLayout(pRightPBVBL);


    QHBoxLayout *pMsgHBL = new QHBoxLayout;
    pMsgHBL->addWidget(m_pInputMsgLE);
    pMsgHBL->addWidget(m_pMsgSendPB);

    m_pOnline = new Online;

    QVBoxLayout *pMain = new QVBoxLayout;
    pMain->addLayout(pTopHBL);
    pMain->addLayout(pMsgHBL);
    pMain->addWidget(m_pOnline);
    m_pOnline->hide();

    setLayout(pMain);
    //关联信号槽
    connect(m_pShowOnlieUsrPB, SIGNAL(clicked(bool)),this,SLOT(showOnline()));
    connect(m_pSearchUsrPB, SIGNAL(clicked(bool)),this,SLOT(searchUsr()));
    connect(m_pFlushFriendPB, SIGNAL(clicked(bool)),this,SLOT(flushFriend()));
    connect(m_pDelFriendPB, SIGNAL(clicked(bool)),this, SLOT(deleteFriend()));
    connect(m_pPrivateChatPB, SIGNAL(clicked(bool)),this, SLOT(privateChat()));
    connect(m_pMsgSendPB, SIGNAL(clicked(bool)),this, SLOT(groupChat()));
}

void Friend::showAllOnlineUsr(PDU *pdu)
{
    if(NULL == pdu){
        return;
    }
    m_pOnline->showUsr(pdu);
}

void Friend::UpdateFriendList(PDU *pdu)
{
    if(NULL == pdu){
        return;
    }
    m_pFriendListWidget->clear();
    uint uiSize = pdu->uiMsgLen/CADATEMAX;
    char caName[CADATEMAX] = {'\0'};
    for(uint i = 0; i < uiSize; i++){
        memcpy(caName, (char*)(pdu->caMsg) + i*CADATEMAX, CADATEMAX);
        m_pFriendListWidget->addItem(caName);
    }
}

void Friend::UpdateGroupMsg(PDU *pdu)
{
    QString strMsg = QString("%1 says: %2").arg(pdu->caData).arg(pdu->caMsg);
    m_pShowMsgTE->append(strMsg);
}

QListWidget *Friend::getFriendList()
{
    return m_pFriendListWidget;
}

void Friend::showOnline()
{
    if(m_pOnline->isHidden())
    {
        m_pOnline->show();

        PDU *pdu = mkPDU(0);
        pdu->uiMsgType = ENUM_MSG_TYPE_ALL_ONLINE_REQUEST;
        TcpClient::getInstance().getTcpSocket().write((char*)pdu, pdu->uiPDULen);
        free(pdu);
        pdu = NULL;
    }else{
        m_pOnline->hide();
    }
}

void Friend::searchUsr()
{
    m_strSearchName =  QInputDialog::getText(this,"搜索", "用户名");
    if(!m_strSearchName.isEmpty()){
        qDebug()<<m_strSearchName;
        PDU *pdu = mkPDU(0);
        pdu->uiMsgType = ENUM_MSG_TYPE_SERACH_USR_REQUEST;
        memcpy(pdu->caData, m_strSearchName.toStdString().c_str(),m_strSearchName.size());
        TcpClient::getInstance().getTcpSocket().write((char*)pdu, pdu->uiPDULen);
        free(pdu);
        pdu = NULL;
    }

}

void Friend::flushFriend()
{
    QString strName = TcpClient::getInstance().getloginName();
    PDU *pdu = mkPDU(0);
    pdu->uiMsgType = ENUM_MSG_TYPE_FLUSH_FRIEND_REQUEST;
    memcpy(pdu->caData, strName.toStdString().c_str(),strName.size());
    TcpClient::getInstance().getTcpSocket().write((char*)pdu, pdu->uiPDULen);
    free(pdu);
    pdu = NULL;
}

void Friend::deleteFriend()
{
    if(NULL == m_pFriendListWidget->currentItem()){
        return;
    }
    QString strFriendName = m_pFriendListWidget->currentItem()->text();
    //qDebug()<<strName;
    QString strSelfName = TcpClient::getInstance().getloginName();
    PDU *pdu = mkPDU(0);
    pdu->uiMsgType = ENUM_MSG_TYPE_DELETE_FRIEND_REQUEST;
    memcpy(pdu->caData,strSelfName.toStdString().c_str(), strSelfName.size());
    memcpy(pdu->caData + CADATEMAX,strFriendName.toStdString().c_str(), strFriendName.size());
    TcpClient::getInstance().getTcpSocket().write((char*)pdu, pdu->uiPDULen);
    free(pdu);
    pdu = NULL;
}

void Friend::privateChat()
{
    if(NULL != m_pFriendListWidget->currentItem()){
        QString strChatName = m_pFriendListWidget->currentItem()->text();
        PrivateChat::getInstance().setWindowTitle(QString(strChatName));
        if(PrivateChat::getInstance().isHidden())
        {
            PrivateChat::getInstance().show();
        }
        PrivateChat::getInstance().setChatName(strChatName);
    }else{
        QMessageBox::warning(this,"私聊", "请选择私聊对象");
    }
}

void Friend::groupChat()
{
    QString strMsg = m_pInputMsgLE->text();
    if(!strMsg.isEmpty())
    {
        PDU *pdu = mkPDU(strMsg.size() + 1);
        pdu->uiMsgType = ENUM_MSG_TYPE_GROUP_CHAT_REQUEST;
        QString strName = TcpClient::getInstance().getloginName();
        strncpy(pdu->caData, strName.toStdString().c_str(), strName.size());
        strncpy(pdu->caMsg, strMsg.toStdString().c_str(), pdu->uiMsgLen);
        TcpClient::getInstance().getTcpSocket().write((char*)pdu, pdu->uiPDULen);
        free(pdu);
        pdu = NULL;
        QString htmlMsg = QString("<div style='color:blue;'>%1 says: %2</div>")
                              .arg(strName)
                              .arg(strMsg);
        m_pShowMsgTE->append(htmlMsg);
        m_pInputMsgLE->clear();
    }
    else
    {
        QMessageBox::warning(this,"群聊","信息不能为空");
    }
}
